package br.com.jk.email;

import br.com.jk.cliente.Cliente;
import br.com.jk.empresa.Empresa;
import br.com.jk.fornecedor.Fornecedor;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author camilo
 */
public class EmailDaoHibernate implements EmailDao {

  private Session session;
  private Transaction transacao;

  public void setSession(Session session) {
    this.session = session;
  }

  @Override
  public void salvar(Email e) {
    this.transacao = this.session.beginTransaction();
    this.session.saveOrUpdate(e);
    this.transacao.commit();
    this.session.close();
  }

  @Override
  public void excluir(Email e) {
    this.transacao = this.session.beginTransaction();
    this.session.delete(e);
    this.transacao.commit();
    this.session.close();
  }

  @Override
  public Email carregar(int id) {
    return (Email) this.session.get(Email.class, id);
  }

  @Override
  public List<Email> listar() {
    return this.session.createCriteria(Email.class).list();
  }
  
  @Override
  public List<Email> listarPorFornecedor(Fornecedor fornecedor){
    Criteria criteria = this.session.createCriteria(Email.class);
    criteria.add(Restrictions.eq("fornecedor", fornecedor));
    return criteria.list();
  }

  @Override
  public List<Email> listarPorFornecedor(Cliente cliente) {
    Criteria criteria = this.session.createCriteria(Email.class);
    criteria.add(Restrictions.eq("cliente", cliente));
    return criteria.list();
  }
  
  @Override
  public List<Email> listarPorEmpresa(Empresa empresa) {
    Criteria criteria = this.session.createCriteria(Email.class);
    criteria.add(Restrictions.eq("empresa", empresa));
    return criteria.list();
  }
}
